<#include "/WEB-INF/views/admin/common/layout/__mainlayout.html">
<#include "/WEB-INF/views/admin/common/__pagination.html">
<#assign navLink='admin:sys:offer:list' />
<#assign pageCss>
<link rel="stylesheet" href="${ctx}/resource/validationengine/css/validationEngine.jquery.css">
</#assign>
<#assign pageScript>
<script type="text/javascript" src="${ctx}/resource/validationengine/js/jquery.validationEngine-zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/resource/validationengine/js/jquery.validationEngine.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	laydate.render({
	    elem: '#search_date'
	    ,range: true
	  });
	
	 $('#search_status').selected({
		    btnWidth: '150px',
		    maxHeight: '300px'
		  });
	 
	 /* $('#search_ownername').selected({
		    btnWidth: '190px',
		    maxHeight: '300px'
		  }); */
		  $('#search_ownername').chosen();
	
	$("#addBtn").click(function(){
		layer.open({
			title:'add offer',
		    type: 2,
		    area: ['800px', '600px'],
		    scrollbar: false,
		    content: ['${ctx}/admin/sys/offer/add_edit','no'],
		    end : function() {
		    	/* var pageNumber = getQueryString('pageNumber');
		    	if(pageNumber==undefined || pageNumber=="" || pageNumber==null){
		    		pageNumber = 1;
		    	}
		    	var pageSize = getQueryString('pageSize');
		    	if(pageSize==undefined || pageSize=="" || pageSize==null){
		    		pageSize = 10;
		    	}
		    	var search = $("input[name=search]").val();
		    	$.ajax({
		    		type:'get',
		    		url:'${ctx}/admin/sys/user/list',
		    		data:'pageNumber='+pageNumber+"&pageSize="+pageSize+"&search="+search,
		    		success:function(){
		    			 
		    		}
		    	}); */
				window.location.reload();
			}
		});
	});
});

function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
} 

function editOffer(id){
	layer.open({
		title:'edit offer',
	    type: 2,
	    area: ['800px', '600px'],
	    scrollbar: false,
	    content: '${ctx}/admin/sys/offer/add_edit?id=' + id,
	    end : function() {
			window.location.reload();
		}
	});
}

function deleteOffer(id) {
	layer.confirm('Are you sure you want to delete it?', {
	    btn: ['yes','no'], //按钮
	   
	}, function(index){
		$.ajax({
			type : "POST",
			url : "${ctx}/admin/sys/offer/delete",
			data : {
				"id" : id
			},
			success : function(userult) {
				if (userult.isSuccess) {
					layer.msg(userult.msg, {
						icon : 1,
						time : 1000,
						end : function() {
							window.location.reload();
						}
					});
				} else {
					layer.msg(userult.msg, {
						time : 1000,
						icon : 2
					});
				}

			}
		});
	    // 提交表单的代码，需要你自己写，然后用 layer.close 关闭就可以了，取消可以省略
	    layer.close(index);
	});
}	

	$("#importExcel").click(function(){
		layer.open({
			title:'import excel',
		    type: 2,
		    area: ['1000px', '600px'],
		    scrollbar: false,
		    content: '${ctx}/admin/sys/offer/importExcel',
		    end : function() {
				window.location.reload();
			}
		});
	});

	function switchStatus(id,status){
		$.ajax({
			type : "POST",
			url : "${ctx}/admin/sys/offer/switchStatus",
			data : {
				"id" : id,
				"status":status
			},
			success : function(userult) {
				if (userult.isSuccess) {
					layer.msg(userult.msg, {
						icon : 1,
						time : 1000,
						end : function() {
							window.location.reload();
						}
					});
				} else {
					layer.msg(userult.msg, {
						time : 1000,
						icon : 2
					});
				}

			}
		});
	
}
</script>
</#assign>
<@mainlayout>
<div class="row-content am-cf">
	<div class="row">
		<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
			<div class="widget am-cf">
				<div class="widget-head am-cf">
					<div class="widget-title am-fl">offer list</div>
					<div class="widget-function am-fr">
						<a href="javascript:;" class="am-icon-cog"></a>
					</div>
				</div>
				<div class="widget-body  widget-body-lg am-fr">
				<div class="am-u-sm-12 am-u-md-4 am-u-lg-4">
						<div class="am-form-group">
							<div class="am-btn-toolbar">
								<div class="am-btn-group am-btn-group-xs">
								<@hasperm key='admin:sys:offer:save'>
									<button type="button" class="am-btn am-btn-default am-btn-success" id="addBtn">
										<span class="am-icon-plus"></span> add
									</button>
								</@hasperm>
								</div><div class="am-btn-group am-btn-group-xs">
									<@hasperm key='admin:sys:offer:save'>
									<button type="button"
										class="am-btn am-btn-default am-btn-success" id="importExcel">
										<span class="am-icon-cloud-upload"></span> import
									</button>
									</@hasperm>
								</div>
								<!-- <div class="am-btn-group am-btn-group-xs">
									<@hasperm key='admin:sys:offer:save'>
									<a href="${ctx}/admin/sys/offer/downfile"
										class="am-btn am-btn-default am-btn-success" id="downloadExcel">
										<span class="am-icon-download"></span> download
									</a>
									</@hasperm>
								</div> -->
								
							</div>
						</div>
					</div>
					<form class="am-form-inline" role="form"
						action="${ctx}/admin/sys/offer/list/" method="post">
						 <div class="am-u-sm-12 am-u-md-2">
							<div
								class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
								 <input
									type="text" class="am-form-field " name="search_id"
									value="${search_id}" placeholder="id">
							</div>
						</div>
						<div class="am-u-sm-12 am-u-md-2">
							<div
								class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
								 <select name="search_status" id="search_status">
										<option value="0">please select status</option>
										<#if search_status?? && search_status == 'published'>
											<option value="published" selected="selected">published</option>
										<#else>
											<option value="published" >published</option>
										</#if>
										<#if search_status?? && search_status == 'suspend'>
											<option value="suspend" selected="selected">suspend</option>
										<#else>
											<option value="suspend" >suspend</option>
										</#if>
									</select>
							</div>
						</div>
						<#if users??>
							<div class="am-u-sm-12 am-u-md-2">
								<div
									class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
									<select name="search_ownername" id="search_ownername">
										<option value="0">please select owner</option>
										<#list users as user>
											<#if search_ownername?? && search_ownername == user.id>
												<option value="${user.id}" selected="selected">${user.name}</option>
											<#else>
												<option value="${user.id}">${user.name}</option>
											</#if>
										</#list>
									</select>
								</div>
							</div>
						</#if>
						<div class="am-u-sm-12 am-u-md-2">
							<div style="width:100%"
								class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
								<input type="text" class="am-form-field"  readonly name="search_date" value="${search_date}" id="search_date" placeholder="create_time">
								<span
									class="am-input-group-btn">
									<button
										class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"
										type="submit"></button>
								</span>
							</div>
						</div>
					</form>
				<div class=" am-scrollable-horizontal" >
                  <table width="100%" class="am-table am-table-compact tpl-table-black am-text-nowrap am-table-hover " id="example-r">
                      <thead>
                          <tr>
                              <th>ID</th>
                              <th>click_url</th>
                               <th>price</th>
                               <th>click_num_limit</th>
                              <th>owner_name</th>
                              <th>create_time</th>
                              <th>update_time</th>
                              <th>status</th>
                              <th>operation</th>
                          </tr>
                      </thead>
                      <tbody>
                      	<#list pageList.list as offer>
                          <tr class="gradeX">
                              <td >${offer.id!}</td>
                              <td >${offer.click_url!}</td>
                              <td >${offer.price!}</td>
                              <td >${offer.click_num_limit!'not limit'}</td>
                              <td>${offer.owner_name!}</td>
                              <td>${offer.create_time!}</td>
                              <td >${offer.update_time!}</td>
                              <td >
                              	<#if offer.status == 'published'><font class="am-text-success">published</font>
                              	<#else><font class="am-text-danger">suspend</font>
                              	</#if>
                              </td>
                              <td>
                              	<#if offer.owner == loginUser.id || session['loginUserRole'].id == '1'>
                                  <div class="tpl-table-black-operation">
                                  <@hasperm key='admin:sys:offer:save'>
                                      <a href="javascript:editOffer('${offer.id}');">
                                          <i class="am-icon-pencil"></i> edit
                                      </a>
                                  </@hasperm>
                                  <@hasperm key='admin:sys:offer:delete'>
                                      <a href="javascript:deleteOffer('${offer.id}');" class="tpl-table-black-operation-del">
                                          <i class="am-icon-trash"></i> delete
                                      </a>
                                  </@hasperm>
                                 <@hasperm key='admin:sys:offer:save'>
                                      <a href="javascript:switchStatus('${offer.id}','${offer.status}');">
                                          <i class="am-icon-edit"></i> <#if offer.status == 'suspend'>published<#else>suspend</#if>
                                      </a>
                                  </@hasperm>
                                  </div>
                                 </#if>
                              </td>
                          </tr>
                          </#list>
                          <!-- more data -->
                      </tbody>
                  </table>
              </div>
              <@pagination page=pageList url='/admin/sys/offer/list/?search_id=${search_id}&search_status=${search_status}&search_date=${search_date}&search_ownername=${search_ownername}' />
              </div>
			</div>
		</div>
	</div>
</div>
</@mainlayout>

